package jxau.cn.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import jxau.cn.dao.LoginDao;
import jxau.cn.db.DBUtil;
import jxau.cn.model.User;
import jxau.xn.servlet.Page;
public class LoginDaoImpl implements LoginDao{
	Connection conn;
	public LoginDaoImpl(){
		conn=DBUtil.getconn();	
	}
	public boolean check(Map<String, String> info) {
		System.out.println(conn);
		PreparedStatement psmt=null;
		String sql="select * from t_user where username=? and psd=?";
		try {
			psmt=conn.prepareStatement(sql);
			ResultSet rs;
			psmt.setString(1,info.get("username"));
			psmt.setString(2,info.get("password"));
			rs=psmt.executeQuery();
			if(rs.next())return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				psmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	public List<User> list(Page fenye) {
		   List<User> list=new ArrayList<User>();
		   PreparedStatement psmt=null;
		   String sql="select count(*) from t_user";
		   String sql2="select * from t_user limit ?,?";
		   try {
			psmt=conn.prepareStatement(sql);
			ResultSet rs=psmt.executeQuery();
			rs.next();
			int count = rs.getInt(1);
			fenye.setCount(count);
			psmt.close();
			psmt=conn.prepareStatement(sql2);
			psmt.setInt(1,fenye.getLow());
			psmt.setInt(2,fenye.getMax());
			ResultSet rs2=psmt.executeQuery();
			while(rs2.next()){
				User u=new User();
				u.setId(rs2.getInt(1));
				u.setUsername(rs2.getString(2));
				u.setPassword(rs2.getString(3));
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				psmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	   return list;
	}
	
      
}
